﻿using AutoMapper;
using Modules.Project;
using PmSoft;
using Modules.System.User;
using Modules.Common;
using PmSoft.ApplicationContext;
using PmSoft.Entity;
using Modules.BankSpecialAccount.Entitys;

namespace ManagementApi.Models.Manage.ProjectSpecialAccount
{
    /// <summary>
    /// 专户收支信息
    /// </summary>
    public class ProjectSpecialAccountPaymentDto : OperateDto
    {
        /// <summary>
        ///
        ///</summary>
        public int Id { get; set; }

        /// <summary>
        /// 流水号
        ///</summary>
        public string? SerialNumber { get; set; }

        /// <summary>
        /// 项目ID
        ///</summary>
        public int ProjectId { get; set; }

        /// <summary>
        /// 项目名称
        ///</summary>
        public string ProjectName { get; set; }

        /// <summary>
        /// 项目编号
        ///</summary>
        public string ProjectNum { get; set; }

        /// <summary>
        /// 银行流水号
        ///</summary>
        public string? BankSerialNumber { get; set; }

        /// <summary>
        /// 收支类型(1：进账 2: 出账)
        ///</summary>
        public 收支类型 IncomeType { get; set; }

        /// <summary>
        /// 收支类型
        /// </summary>
        public string? IncomeTypeName { get; set; }

        /// <summary>
        /// 交易类型（100：资金入账 102：代发工资 103：计息 104：手续费 105：其他）
        ///</summary>
        public 交易类型 TradeType { get; set; }

        /// <summary>
        /// 交易类型
        /// </summary>
        public string? TradeTypeName { get; set; }

        /// <summary>
        /// 账户名称
        ///</summary>
        public string? AccountName { get; set; }

        /// <summary>
        /// 银行账号
        ///</summary>
        public string? BankNo { get; set; }

        /// <summary>
        /// 对方户名
        ///</summary>
        public string? OppositeName { get; set; }

        /// <summary>
        /// 对方账户
        ///</summary>
        public string? OppositeAccount { get; set; }

        /// <summary>
        /// 收支金额
        ///</summary>
        public decimal Amount { get; set; }

        /// <summary>
        /// 收支日期
        /// </summary>
        public DateTime? IncomeDate { get; set; }

        /// <summary>
        /// 余额
        ///</summary>
        public decimal? Balance { get; set; }

        /// <summary>
        /// 收支日期
        ///</summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 交易币种
        /// </summary>
        public string TradeCurrency { get; set; } = string.Empty;

        /// <summary>
        /// 交易摘要
        /// </summary>
        public string TradAbstract { get; set; } = string.Empty;

        /// <summary>
        /// 交易备注
        /// </summary>
        public string TradRemarks { get; set; } = string.Empty;
    }

    public static partial class ProjectSpecialAccountPaymentExceptions
    {
        /// <summary>
        /// 组装Dto信息
        /// </summary>
        /// <param name="projectSpecialAccountPayment"></param>
        /// <returns></returns>
        public static ProjectSpecialAccountPaymentDto AsProjectSpecialAccountPaymentDto(this BankSpecialAccountPayment projectSpecialAccountPayment, IApplicationContext applicationContext)
        {
            ProjectSpecialAccountPaymentDto dto = new ProjectSpecialAccountPaymentDto();
            var projectService = applicationContext.GetService<ProjectService>();
            var mapper = applicationContext.GetService<IMapper>();
            mapper.Map(projectSpecialAccountPayment, dto);
            dto.IncomeTypeName = projectSpecialAccountPayment.IncomeType.GetDescription();
            dto.TradeTypeName = projectSpecialAccountPayment.TradeType.GetDescription();
            var projectInfo = projectService.GetProjectInfo(dto.ProjectId);
            if (projectInfo != null)
            {
                dto.ProjectName = projectInfo.Name;
                dto.ProjectNum = projectInfo.PrjNum;

                #region 主管单位数据权限

                if (applicationContext.CurrentUser is IDeptUser deptUser)
                {
                    if (deptUser.IsSuperAdministrator() || projectInfo.DeptId == deptUser.DeptId)
                        dto.HasEdit = true;
                    else dto.HasEdit = false;
                }

                #endregion 主管单位数据权限
            }
            return dto;
        }

        public static List<Dictionary<string, string>> AsExportDto(this IEnumerable<BankSpecialAccountPayment> list)
        {
            List<Dictionary<string, string>> exportDatas = new List<Dictionary<string, string>>();
            var projectService = ServiceAccessor.GetService<ProjectService>();
            var projects = projectService.GetProjectInfoByProjectIds(list.Select(a => a.ProjectId));

            foreach (var item in list)
            {
                Dictionary<string, string> exportData = new Dictionary<string, string>();
                exportData["项目编号"] = projects.FirstOrDefault(a => a.ProjectId == item.ProjectId)?.PrjNum ?? string.Empty;
                exportData["项目名称"] = projects.FirstOrDefault(a => a.ProjectId == item.ProjectId)?.Name ?? string.Empty;
                exportData["流水号"] = item.SerialNumber;
                exportData["银行账号"] = item.BankNo;
                exportData["交易流水号"] = item.BankSerialNumber;
                exportData["收支类型"] = item.IncomeType.GetDescription();
                exportData["交易类型"] = item.TradeType.GetDescription();
                exportData["交易币种"] = item.TradeCurrency;
                exportData["交易摘要"] = item.TradAbstract;
                exportData["交易备注"] = item.TradRemarks;
                exportData["账户名称"] = item.AccountName;
                exportData["账户余额"] = item.Balance.ToString("0.00") ?? string.Empty;
                exportData["收支金额"] = item.Amount.ToString("0.00");
                exportData["收支日期"] = item.CreateTime.ToString("yyyy-MM-dd") ?? string.Empty;
                exportDatas.Add(exportData);
            }
            return exportDatas;
        }
    }
}